// 导入数据库操作模块,一般叫做db
const db = require('../db')

exports.Search = async (req, res) => {
  class Search {
    parameter = {}
    // 标记-- 新增加的条件

    /*  idCard : 身份证号/订单号, 
        email : 邮箱, 
        ordersStatus : 订单状态, 
        area : 地区, 
        department : 部门, 
        processStatus : 进程状态, 
        creationDate : 创建日期, 
        warehouseNumber : 仓库编号, 
        unit : 单位 , 
        number : 数字,
        gender : 性别,
        address :  地址, 
        code : 编码, 
        avatar : 头像, 
        isdelete :是否删除 
        statusid : 链接另一张表的ID,
        username :用户名  */
    constructor({ idCard, email, ordersStatus, area, department, processStatus, creationDate, warehouseNumber, unit, number, gender, address, code, avatar, isdelete, statusid, username }) {
      if (idCard) this.parameter.idCard = idCard
      if (email) this.parameter.email = email
      if (ordersStatus) this.parameter.ordersStatus = ordersStatus
      if (area) this.parameter.area = area
      if (department) this.parameter.department = department
      if (processStatus) this.parameter.processStatus = processStatus
      if (creationDate) this.parameter.creationDate = creationDate
      if (warehouseNumber) this.parameter.warehouseNumber = warehouseNumber
      if (unit) this.parameter.unit = unit
      if (number) this.parameter.number = number
      if (gender) this.parameter.gender = gender
      if (address) this.parameter.address = address
      if (code) this.parameter.code = code
      if (avatar) this.parameter.avatar = avatar
      if (isdelete) this.parameter.isdelete = isdelete
      if (statusid) this.parameter.statusid = statusid
      if (username) this.parameter.username = username
    }
    where() {
      let sql = ''
      Object.keys(this.parameter).forEach((parameterElement, index) => {
        sql += `and ${parameterElement} like '%${this.parameter[parameterElement]}%'`
      })
      return `select * from var_table_main where isdelete=0 ` + sql + ` order by id asc limit ?,?`
    }
  }
  let search = new Search(req.body)
  const sql_Jincheng = `select * from var_table_jinchengzhuangtai_fen where isdelete=0 order by id asc`
  const sql_fenye = search.where()
  const sqlTotal = 'select count(*) as total from var_table_main' //as更换名称
  // 前端不传参数默认 返回 0 10
  // 页码
  const page = +req.query.page || 1
  // 修改--条数
  const pagesize = +req.query.pagesize || 10
  // 总条数
  const params = [(Number(page) - 1) * Number(pagesize), Number(pagesize)]
  // 主
  Promise.all([p(sql_fenye, params), p(sql_Jincheng), p(sqlTotal)])
    .then(([result_fenye, result_2, count]) => {
      // console.log('🚚...  信息', count)
      // console.log('🚚...  信息', result_fenye)
      // console.log('🚚...  信息', result_2)
      let total = count[0]['total'] //查询表中的数量
      // 修改--
      let arr = {
        code: 0,
        message: '获取商品分类成功',
        data: {
          list: result_fenye,
          page,
          pagesize,
          total: total
        }
      }
      // 修改--
      arr.data.list.map((item, i) => {
        item.list = result_2
      })
      res.send(arr)
    })
    .catch(err => {
      console.log('err', err.message)
      res.json({
        code: 1,
        data: {
          list: []
        },
        message: '查询暂无结果'
      })
    })
}

function p(sql, params) {
  return new Promise((resolve, reject) => {
    db.query(sql, params, (err, result) => {
      if (err) return reject(err)
      resolve(result)
    })
  })
}
